/**
 * Created by Rossy1 on 2018/4/26.
 */
document.documentElement.style.fontSize = document.documentElement.clientWidth / 3.75 + "px"; //兼容处理

var url = window.location.href.split("?")[1];
var curPage = 1;
var allPage;
var UserInfo;
var infos = sessionStorage.getItem("UserInfo"); //主页信息
var mark;
var allhisSong=[];
//获取歌曲信息
if (url != undefined) { //主页进来
    infos=JSON.parse(infos);
    $("#headimg").attr("src", infos[1]); //头像
    $("#headright").html(infos[0]); //昵称
    $(".footer").hide();
    $(".header").css("margin-top","0.2rem")
    var uid=url.split("&")[1].substr(4);
    mark=url.split("&")[0].substr(5);
    $("#uid").html(uid); //uid
    getWorks(uid,mark);

} else {
    getSong(curPage, 1);
}

function getSong(page, page_size) {
    $.ajax({
        type: "GET",
        url: "http://14.119.109.232:30666/RecordFile/",
        data: {
            page: page,
            page_size: page_size
        },
        success: function (res) {
            console.log(res)
            allPage = res.count;
            UserInfo = res.results[0].UserInfo;
            $("#audio").attr("src", "http://" + res.results[0].recordUrl);
            autoPlayAudio();
            getTotalTime();
            //设置页面信息
            $("#headimg").attr("src", res.results[0].UserInfo[1]); //头像
            $("#backImg").attr("src", "http://" + res.results[0].picUrl); //背景图
            $("#bimg").attr("src", "http://" + res.results[0].picUrl); //旋转图
            $("#headright").html(res.results[0].UserInfo[0]); //昵称
            $("#musicname").html(res.results[0].songInfo[0]); //歌曲名
            $("#musiscore").html(res.results[0].score + " 分"); //得分
            $("#loveNum").html(res.results[0].likesCount); //喜欢次数
            $("#uid").html(res.results[0].uid); //uid

        },
        error: function (res) {

        }
    });
}

function getWorks(uid,mark) {
    $.ajax({
        type: "GET",
        url: "http://14.119.109.232:30666/center/wechat/",
        data: {
            cmd: "UserDetail",
            uid: uid
        },
        success: function (res) {
            console.log(res)
            setInfo(res.records,mark);
            UserInfo = infos;
            allhisSong=res.records;

        },
        error: function (res) {

        }
    });
}

function setInfo(res,mark) {
    $("#audio").attr("src", "http://" + res[mark].recordUrl);
    $("#backImg").attr("src", "http://" + res[mark].picUrl); //背景图
    $("#bimg").attr("src", "http://" + res[mark].picUrl); //旋转图
    $("#musicname").html(res[mark].song_info.name); //歌曲名
    $("#musiscore").html(res[mark].score + "分"); //得分
    $("#loveNum").html(res[mark].likesCount); //喜欢次数
    autoPlayAudio();
    getTotalTime();
}

function autoPlayAudio() {
    document.getElementById("audio").play();
    $("#chpdiv").addClass("rainbow");
    wx.config({
        debug: false,
        appId: '',
        timestamp: 1,
        nonceStr: '',
        signature: '',
        jsApiList: []
    });
    wx.ready(function () {
        document.getElementById('audio').play();
    });
}

//获取歌曲当前时间
function setCurTime() {
    var currentTime = document.getElementById("audio").currentTime;
    $("#curTime").html(setTime(currentTime));
}
var t = setInterval(function () {
    setCurTime();
}, 500);
// 获取歌曲总时间
var timer;
function getTotalTime() {
    timer = setInterval(function () {
        var durationTime = document.getElementById("audio").duration;
        if (!isNaN(durationTime)) {
            $("#totalTime").html(setTime(durationTime));
            clearInterval(timer);
        }
    }, 500);
}
// 时间格式化00:00
function setTime(x) {
    var m = Math.floor(x / 60);
    var s = x.toFixed(0) - m * 60;
    m = m < 10 ? "0" + m : m;
    s = s < 10 ? "0" + s : s;
    return m + ":" + s;
}

//暂停/播放
var f = true;
$("#pause").on("click", function () {
    if (f) {
        $("#audio")[0].pause();
        $("#chpdiv").removeClass("rainbow");
        clearInterval(t);
        $("#pause").attr("src", "img/z02.png");
        f = false;
    } else {
        $("#audio")[0].play();
        $("#chpdiv").addClass("rainbow");
        t = setInterval(function () {
            setCurTime();
        }, 500);
        $("#pause").attr("src", "img/b02.png");
        f = true;
    }
});

//是否播放完成
var audio = document.getElementById("audio");
audio.addEventListener('ended', function () {
    //完成后下一曲
    if (url != undefined){
        if(mark==allhisSong.length-1){
            mark=0
        }else{
            mark++
        }
        setInfo(allhisSong,mark);
    }else{
        if (curPage == allPage) {
            curPage = 1
        } else {
            curPage++;
        }
        getSong(curPage, 1);
    }
}, false);

//上一曲
$("#pre").on("click", function () {
    if (url != undefined){
        if(mark==0){
            mark=allhisSong.length-1
        }else{
            mark--
        }
        setInfo(allhisSong,mark);
    }else{
        if (curPage == 1) {
            curPage = allPage;
        } else {
            curPage--;
        }
        getSong(curPage, 1);
    }
})
//下一曲
$("#next").on("click", function () {
    if (url != undefined){
        if(mark==allhisSong.length-1){
            mark=0
        }else{
            mark++
        }
        setInfo(allhisSong,mark);
    }else{
        if (curPage == allPage) {
            curPage = 1
        } else {
            curPage++;
        }
        getSong(curPage, 1);
    }
})


//点击歌手头像跳转他的主页
$("#headleft").on("click", function () {
    var uid = $(this).children("#uid").text();
    sessionStorage.setItem("uid", uid);
    sessionStorage.setItem("UserInfo", JSON.stringify(UserInfo));
    window.location.href = "hisHomePage.html?his";
})

//点击我的跳转我的主页
$("#mymusic").on("click", function () {

    window.location.href = "hisHomePage.html?my";
})

//点击喜欢按钮
$("#likes").on("click", function () {
    var loveImg = $("#loveImg")[0].currentSrc;
    var loveNum = $("#loveNum").html(); //喜欢的数字
    if (loveImg.indexOf("love1.png") > 0) { //喜欢
        $("#loveImg").attr("src", "img/love2.png")
        loveNum++;
    } else if (loveImg.indexOf("love2.png") > 0) { //取消喜欢
        $("#loveImg").attr("src", "img/love1.png")
        loveNum--;
    }
    $("#loveNum").html(loveNum);
})

//点击原唱
$("#origin").on("click", function () {

    window.location.href = "sings.html";
})

//关注/取消关注
$("#calfollow").on("click", function () {
    var followTxt = $("#calfollow").text();
    if (followTxt == "取消关注") {
        $("#calfollow").text("关注");
        $("#calfollow").css("background", "#6e93c9");
    } else if (followTxt == "关注") {
        $("#calfollow").text("取消关注");
        $("#calfollow").css("background", "#7f7f81");
    }
})


